<?php
class Admin_RemindController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $tWhere = ' 1 ';
        $pName = empty($p['name'])?'':Tool_Fnc::safe_string($p['name']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);

        $tSMO = new SeniorModel;
        $tRMO = new RemindModel;
        $tSIMO = new SexinfoModel;
        $tDIMO = new DepartmentinfoModel;
        $tEIMO = new EventinfoModel; 

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pDepartmentID = empty($p['department_id'])?Tool_Fnc::ajaxMsg('系统错误'):Tool_Fnc::safe_string($p['department_id']);
            //$tDatas = $tSMO->field('id,name')->where('department = '.$pDepartmentID)->fList();
            $tSql = 'select id,name from '.$tSMO->table.' where department = '.$pDepartmentID;
            $tDatas = $tSMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
        if(!empty($pName)){
            $tWhere .= ' and s.name like \'%'.$pName.'%\'';
        }
        if(!empty($pDepartment)){
            $tWhere .= ' and s.department like \'%'.$pDepartment.'%\'';
        }
        //$tCount = $tSMO->field('count(*) c')->where($tWhere)->fList();
        $tSql = 'select count(*) c from '.$tRMO->table.' r left join '.$tSMO->table.' s on s.id = r.sid left join '.$tDIMO->table.' di on s.department = di.num where '.$tWhere;
        $tCount = $tRMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tSql = 'select r.*,s.name name,s.sex sex,di.introduce department_introduce from '.$tRMO->table.' r left join '.$tSMO->table.' s on s.id = r.sid left join '.$tDIMO->table.' di on s.department = di.num where '.$tWhere.' order by id desc limit '.$tPage->limit();
            $tDatas = $tRMO->query($tSql);
        }
        //所在部门
        $tDDatas = $tDIMO->field('num,introduce')->fList(); 
        //老人显示
        $tSDatas = $tSMO->field('id,name')->fList();
        //事件类型显示
        $tEDatas = $tEIMO->field('num,introduce')->fList();
        $this->assign('pName',$pName);
        $this->assign('pDepartment',$pDepartment);
        $this->assign('tEDatas',$tEDatas);
        $this->assign('tSDatas',$tSDatas);
        $this->assign('tDDatas',$tDDatas);
        $this->assign('getSex',$tSIMO->tSexinfo);
        $this->assign('tDatas',$tDatas);
        $this->assign('tShow',$tPage->show());
    }
    public function addAction(){
        $p = $_REQUEST;
        $pSenior = empty($p['senior'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['senior']);
        $pRemindtype = empty($p['remind_type'])?'':Tool_Fnc::safe_string($p['remind_type']);
        $pNewstype = empty($p['news_type'])?'':Tool_Fnc::safe_string($p['news_type']);
        $pStartdate = empty($p['startdate'])?'':Tool_Fnc::safe_string($p['startdate']);
        $pEnddate = empty($p['enddate'])?'':Tool_Fnc::safe_string($p['enddate']);
        $pRespondtime = empty($p['respond_time'])?'':Tool_Fnc::safe_string($p['respond_time']);
        $pDescription = empty($p['description'])?'':Tool_Fnc::safe_string($p['description']);
        $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);
        
        //查询绑定护工信息
        $tWSMO = new WorkerseniorModel;
        $tRMO = new RemindModel;

        $tWSRow = $tWSMO->field('sw_id')->where('sid = '.$pSenior)->fRow();
        if(empty($tWSRow)){Tool_Fnc::ajaxMsg('该老人没有绑定护工');}

        $tStarttime = strtotime($pStartdate.' '.$pRespondtime);
        $tEndtime = strtotime($pEnddate.' '.$pRespondtime); 

        $tTime = time();
        $tData = array(
            'sid' => $pSenior,
            'remind_type' => $pRemindtype,
            'event_type' => $pNewstype,
            'sw_id' => $tWSRow['sw_id'],
            'startdate' => strtotime($pStartdate),
            'enddate' => strtotime($pEnddate),
            'starttime' => $tStarttime,
            'endtime' => $tEndtime,
            'respond_time' => $pRespondtime,
            'description' => $pDescription,
            'remarks' => $pRemarks,
            'created' => $tTime,
    );
        if(!$tId = $tRMO->insert($tData)){
            Tool_Fnc::ajaxMsg('添加失败');
        }
        Tool_Fnc::ajaxMsg('添加成功',1);
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);

        $tRMO = new RemindModel;
        $tSql = 'delete from '.$tRMO->table.' where id = '.$pId;
        if(!$tRMO->exec($tSql)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    } 
}
?>
